package mvc.controller;

import mvc.model.pojos.CartBean;
import mvc.model.pojos.UserBean;
import mvc.model.service.CartService;
import mvc.model.service.impl.CartServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "UpCartServlet", urlPatterns = "/upcart")
public class UpCartServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int count = Integer.parseInt(request.getParameter("count"));
        int cartnumber = Integer.parseInt(request.getParameter("cartnumber"));
        int checked = Integer.parseInt(request.getParameter("checked"));
        CartService cartService = new CartServiceImpl();
        CartBean cartBean = cartService.queryOne(cartnumber);
        cartBean.setCount(count);
        cartBean.setChecked(checked);

        cartService.upCart(cartBean, cartnumber);
        response.getWriter().append("ok");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        UserBean user = (UserBean) request.getSession().getAttribute("user");
        int checked = Integer.parseInt(request.getParameter("checked"));
        CartService cartService = new CartServiceImpl();
        cartService.upCartAll(user.getUid(), checked);
        response.getWriter().append("ok");
    }
}
